package com.job;

import com.common.utils.StringUtils;
import com.common.utils.nrmm.NrmmUtils;
import com.model.domain.vecc.GetVeccRequestLogInfoDO;
import com.model.dto.common.TerminalRecordGetIdentifierByIdDTO;
import com.model.pojo.user.UserInfo;
import com.model.pojo.vecc.VeccDeviceModelAuthRecordInfo;
import com.model.pojo.vecc.VeccRequestLogInfo;
import com.model.vo.common.VeccReturnResultVO;
import com.model.vo.common.VeccReturnVO;
import com.service.system.SystemDeviceModelInfoService;
import com.service.user.UserInfoService;
import com.service.vecc.VeccDeviceModelAuthRecordInfoService;
import com.service.vecc.VeccRequestLogInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * TODO
 *
 * @Description
 * @Author tm
 * @Date 2024-03-02 23:13
 **/
@Slf4j
@Component
@RequiredArgsConstructor
public class VeccInfoJob {
    private final VeccDeviceModelAuthRecordInfoService veccDeviceModelAuthRecordInfoService;
    private final UserInfoService userInfoService;
    private final SystemDeviceModelInfoService systemDeviceModelInfoService;
    private final VeccRequestLogInfoService veccRequestLogInfoService;
    /**
     * 更新农机设备的在线状态
     * @author tm
     * @date 2023-05-27 10:10
     */
    @Scheduled(cron = "0 */5 * * * ?")
    public void updateRequestLog(){
        GetVeccRequestLogInfoDO getVeccRequestLogInfoDO = new GetVeccRequestLogInfoDO();
        getVeccRequestLogInfoDO.setStatus(0);
        List<VeccRequestLogInfo> logList = veccRequestLogInfoService.getAllList(getVeccRequestLogInfoDO);
        if(logList!=null && logList.size()>0){
            for(VeccRequestLogInfo info:logList){
                if(StringUtils.isBlank(info.getRequestId()))continue;
                VeccDeviceModelAuthRecordInfo recordInfo = veccDeviceModelAuthRecordInfoService.getById(info.getRecordId());
                if(recordInfo == null){
                    continue;
                }
                UserInfo userInfo = userInfoService.getById(recordInfo.getFactoryUserId());
                if(userInfo == null || StringUtils.isBlank(userInfo.getForwardAccount())||StringUtils.isBlank(userInfo.getForwardPwd())){
                    continue;
                }
                String token = "";
                VeccReturnVO loginVo = NrmmUtils.recordLogin(userInfo.getForwardAccount(),userInfo.getForwardPwd());
                if(loginVo.getCode() == 200 &&StringUtils.isNotBlank(loginVo.getResultVO().getToken())){
                    token = loginVo.getResultVO().getToken();
                }
                if(StringUtils.isBlank(token)){
                    continue;
                }
                if(info.getType() == 4) {//终端授权备案
                    TerminalRecordGetIdentifierByIdDTO terminalRecordGetIdentifierByIdDTO = new TerminalRecordGetIdentifierByIdDTO();
                    terminalRecordGetIdentifierByIdDTO.setId(info.getRequestId());
                    VeccReturnVO veccReturnVO = NrmmUtils.terminalRecordGetIdentifierById(terminalRecordGetIdentifierByIdDTO, token);
                    if(veccReturnVO!=null){
                        VeccRequestLogInfo logInfo = new VeccRequestLogInfo();
                        logInfo.setRequestId(info.getRequestId());
                        logInfo.setRecordId(info.getRecordId());
                        logInfo.setRequestUrl(veccReturnVO.getRequestVO().getRequestUrl());
                        logInfo.setContent(veccReturnVO.getRequestVO().getContent());
                        logInfo.setResult(veccReturnVO.getRequestVO().getResult());
                        logInfo.setType(5);
                        logInfo.setStatus(1);
                        veccRequestLogInfoService.save(logInfo);
                    }
                    int status = 0;
                    String remark = "";
                    if(veccReturnVO.getCode() == 200){
                        VeccReturnResultVO resultVO = veccReturnVO.getResultVO();
                        if("0".equals(resultVO.getState()))continue;//确认
                        else{
                            status = Integer.valueOf(resultVO.getState());
                            remark = resultVO.getDetail();
                        }
                    }
                    info.setStatus(status);
                    info.setRemark(remark);
                    info.setAuditTime(new Date());
                    veccRequestLogInfoService.update(info);
                    recordInfo.setStatus(info.getStatus()+1);
                    recordInfo.setAuthTime(new Date());
                    veccDeviceModelAuthRecordInfoService.update(recordInfo);
                }
            }
        }
    }
}
